package cn.bluethink.eguan.okrtask.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.beans.BeanUtils;

import cn.bluethink.eguan.model.core.EgOType;
import cn.bluethink.eguan.model.core.EgObject;
import cn.bluethink.eguan.model.core.EgTask;
import cn.bluethink.eguan.model.core.EgUser;
import cn.bluethink.eguan.model.okrtask.EgOkrTask;

/**
 * @author onegis
 *
 */
@Table(name = "eg_okrtask")
public class EgOKRTaskEntity implements Serializable {

    private static final long serialVersionUID = 196115844712423286L;

    @Id
    private Long tid;

    private Integer timing;

    private Long douid;

    // 验收意见
    private String evaluate;

    @Column(name = "exp_skp")
    private Integer expSkp;

    @Column(name = "result_oid")
    private Long resultOid;

    private Integer sort;

    private Long parent;

    private Integer color;

    public EgOKRTaskEntity() {

    }

    public EgOKRTaskEntity(EgOkrTask okrTask, boolean bcreate) {
        this.tid = okrTask.getId();
        this.douid = (okrTask.getDouser() == null || okrTask.getDouser().getId() == null
                || okrTask.getDouser().getId() == 0) ? null : okrTask.getDouser().getId();
        this.resultOid = okrTask.getResult() == null ? null : okrTask.getResult().getId();
        // 增加验收意见
        this.evaluate = okrTask.getEvaluate() == null ? "" : okrTask.getEvaluate();
        this.timing = okrTask.getTiming() == null ? 0 : okrTask.getTiming();
        if (bcreate) {
            this.expSkp = okrTask.getExpSkp() == null ? 0 : okrTask.getExpSkp();
            this.color = okrTask.getColor() == null ? null : okrTask.getColor();
        } else {
            this.expSkp = okrTask.getExpSkp();
            this.color = okrTask.getColor();
        }
        this.parent = okrTask.getParent() == null ? null : okrTask.getParent().getId();

    }

    /**
     * 转换为工作任务模型
     * 
     * @param entity
     * @param task
     * @return
     */
    public EgOkrTask createOkr(EgTask task) {
        EgOkrTask okrTask = new EgOkrTask(tid, null, null);
        BeanUtils.copyProperties(task, okrTask);

        okrTask.setTiming(timing);

        if (douid != null && douid > 0) {
            okrTask.setDouser(new EgUser(douid, null, null));
        } else {
            okrTask.setDouser(new EgUser());
        }
        okrTask.setEvaluate(evaluate);
        okrTask.setExpSkp(expSkp);

        okrTask.setResult(null);
        if (resultOid != null && resultOid > 0) {
            okrTask.setResult(new EgObject(resultOid, null, new EgOType(EgOType.FILE_LIST, null)));
        }
        // okrTask.setBtrain(this.istrain);
        if (parent != null && parent > 0) {
            okrTask.setParent(new EgOkrTask(parent, null, null));
        }
        if (color != null) {
            okrTask.setColor(color);
        }
        return okrTask;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public Integer getTiming() {
        return timing;
    }

    public void setTiming(Integer timing) {
        this.timing = timing;
    }

    public Long getDouid() {
        return douid;
    }

    public void setDouid(Long douid) {
        this.douid = douid;
    }

    public String getEvaluate() {
        return evaluate;
    }

    public void setEvaluate(String evaluate) {
        this.evaluate = evaluate;
    }

    public Integer getExpSkp() {
        return expSkp;
    }

    public void setExpSkp(Integer expSkp) {
        this.expSkp = expSkp;
    }

    public Long getResultOid() {
        return resultOid;
    }

    public void setResultOid(Long resultOid) {
        this.resultOid = resultOid;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public Long getParent() {
        return parent;
    }

    public void setParent(Long parent) {
        this.parent = parent;
    }

    public Integer getColor() {
        return color;
    }

    public void setColor(Integer color) {
        this.color = color;
    }

}
